<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Adproject;
use app\common\model\Adzone;

//use think\Config;

/**
 * 广告位管理
 *
 * @icon fa fa-circle-o
 */
class Adszone extends Api {

	// 无需登录的接口,*表示全部
	protected $noNeedLogin = '*';
	// 无需鉴权的接口,*表示全部
	protected $noNeedRight = '*';

    /**
     * @var $Adzone Adzone
     */
	protected $Adzone = null;
    /**
     * @var $Adproject Adproject
     */
	protected $Adproject = null;

	public function _initialize() {
		parent::_initialize();
		$this->Adzone = new Adzone();
		$this->Adproject = new Adproject();
	}

	public function getAdsByMark() {
		$mark = $this->request->request('mark');
		if ($mark == "") {
			$this->error(__('Invalid parameters'));
		}
		$result = $this->Adzone->getByMark($mark);
		if ($result) {
		    $where['zone_id'] = $result->id;
		    $where['effectime'] = ['<', time()];
		    $where['expiretime'] = ['>', time()];
            $result->lists = $this->Adproject->with('demand.user,goods.user')->where($where)->order('weigh','desc')->select();
            $this->success('返回成功', $result);
		} else {
			$this->error("广告位不存在");
		}
	}

	public function getAdsById() {
		$mark = $this->request->request('id');
		if ($mark == "") {
			$this->error(__('Invalid parameters'));
		}
		$result = $this->Adzone->getByMark($mark);
		if ($result) {
		    $where['zone_id'] = $result->id;
		    $where['effectime'] = ['<', time()];
		    $where['expiretime'] = ['>', time()];
            $result->lists = $this->Adproject->with('demand.user,goods.user')->where($where)->order('weigh','desc')->select();
            $this->success('返回成功', $result);
		} else {
			$this->error("广告位不存在");
		}
	}

}
